package cn.weekimwee.firstlineofcodeforkotlin

import android.content.IntentFilter
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import cn.weekimwee.firstlineofcodeforkotlin.unit5.GoBackReceiver

/**
 * Created by Wee Kim Wee on 2018/2/8.
 */
open class BaseActivity : AppCompatActivity(){

    private  var receiver: GoBackReceiver?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("BaseActivity",javaClass.simpleName)
        ActivityCollector.addActivity(this)
    }

    /**
     * 这两个方法中注册广播接收器，因为只需要栈顶的接收广播就可以了。如果执行了被挤掉的操作的话，在广播接收器中会将其他的activity也一起finish掉
     */
    override fun onResume() {
        super.onResume()
        receiver = GoBackReceiver()
        registerReceiver(receiver, IntentFilter().apply {
            addAction("cn.weekimwee.firstlineofcodeforkotlin.goback")
        })
    }

    override fun onPause() {
        super.onPause()
        if(receiver!=null) unregisterReceiver(receiver)
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
    }
}